﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zmMoniter
{
    public partial class frmNonVibrator : Form
    {
        public string sDeckID="";
        public string sLayerID="";
        public string sName = "";
        public double dArea=0;
        public string sExplan="";
        public long lColor = 0;
        public string sVibrPoints = "";
        public string sNonVibrID = "";
        public frmNonVibrator()
        {
            InitializeComponent();
            setFlag();
        }
        public void reSetData()
        {
            sDeckID="";
            sLayerID="";
            sName = "";
            dArea=0;
            sExplan="";
            lColor = 0;
            sVibrPoints = "";
            sNonVibrID = "";
            setFlag();
        }
        public void setData(string DeckID, string LayerID, string VibrPoints)
        {
            reSetData();
            sDeckID = DeckID;
            sLayerID = LayerID;
            sVibrPoints = VibrPoints;
        }
        private void sBtn_OK_Click(object sender, EventArgs e)
        {
            if(tEdt_Name.Text=="")
            {
                MessageBox.Show("请输入非振捣区域名称！", "非振捣区域信息保存提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            /* string sSQL = "SELECT  非振捣区域自增长.nextval AS maxid FROM dual";
           
             DataTable dt = db.ReturnDataTable(sSQL);
             int iMaxID = -1;
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 DataRow dataRow = dt.Rows[i];
                 iMaxID = int.Parse(dataRow["maxid"].ToString());
             }
             if (iMaxID == -1)
                 return;
                */

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();

             string sSQL = "INSERT INTO 非振捣区域 (编号,非振捣点位,名称,说明,面积," +
             "仓面号,层号,显示颜色)" +
             "VALUES (非振捣区域自增长.nextval"+ ",'" + sVibrPoints + "','" + 
             tEdt_Name.Text + "','" + tEdt_Explain.Text +
             "'," + (tEdt_Area.Text == "" ? "null" : tEdt_Area.Text) + "," +
             sDeckID + "," + sLayerID + "," + cEdt_Color.Color.ToArgb() + ")";

            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                //MessageBox.Show("添加非振捣区域成功！", "添加非振捣区域提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reSetData();
                db.CloseConn();
                this.DialogResult = DialogResult.OK;
            }
            else
            {
               // MessageBox.Show("添加非振捣区域失败！", "添加非振捣区域提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                db.CloseConn();
            }
        }
        private void sBtn_Delete_Click(object sender, EventArgs e)
        {
            if(sNonVibrID!="")
            {
                PublicDataCls db = new PublicDataCls();
                db.OpenConn();

                string sSQL = "Delete 非振捣区域 WHERE 编号=" + sNonVibrID;

                int iResult = db.ExecuteSQL(sSQL);
                if (iResult > 0)
                {
                    reSetData();
                    db.CloseConn();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    // MessageBox.Show("添加非振捣区域失败！", "添加非振捣区域提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    db.CloseConn();
                }
            }
        }
        public void setValue(string sName, string sDisc,string sArea,Color cl)
        {
            tEdt_Name.Text = sName;
            tEdt_Explain.Text = sDisc;
            tEdt_Area.Text = sArea;

            cEdt_Color.Color = cl;
        }
        public void setID(string sID)
        {
            sNonVibrID=sID;
        }
        public void setFlag(string sFlag = "add")
        {
            if (sFlag == "edit")
            {
                sBtn_Delete.Visible = true;
                this.Text = "编辑非振捣区域";
                sBtn_OK.Visible = false;
                sBtn_Save.Visible = true;
            }
            if (sFlag == "add")
            {
                sBtn_Delete.Visible = false;
                this.Text = "添加非振捣区域";
                sBtn_OK.Visible = true;
                sBtn_Save.Visible = false;
            }
        }
        private void sBtn_Save_Click(object sender, EventArgs e)
        {
            if (tEdt_Name.Text == "")
            {
                MessageBox.Show("请输入非振捣区域名称！", "非振捣区域信息保存提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "UPDATE 非振捣区域 SET 名称='" + tEdt_Name.Text + "',说明='" +
                tEdt_Explain.Text + "',面积=" + (tEdt_Area.Text == "" ? "null" : tEdt_Area.Text) +
                ",显示颜色=" + cEdt_Color.Color.ToArgb()+ " WHERE 编号=" + sNonVibrID;


            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                //MessageBox.Show("保存非振捣区域成功！", "保存非振捣区域提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reSetData();
                db.CloseConn();
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                // MessageBox.Show("保存非振捣区域失败！", "保存非振捣区域提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                db.CloseConn();
            }
        }
     
    }
}
